package com.ssd.controller.operation;


import com.ssd.entity.operation.dto.LoginDto;
import com.ssd.entity.operation.entity.SysUser;
import com.ssd.entity.operation.vo.LoginVo;
import com.ssd.entity.operation.vo.SysMenuVo;
import com.ssd.entity.operation.vo.ValidateCodeVo;
import com.ssd.service.SysMenuService;
import com.ssd.service.SysUserService;
import com.ssd.service.ValidateCodeService;
import com.ssd.utils.UserContext;
import com.ssd.view.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Api(tags = "用户接口")
@RestController
@RequestMapping(value = "/admin/index")
public class IndexController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService menuService;

    //查询用户可以操作菜单
    @GetMapping("/menus")
    @ApiOperation(value = "查询用户可以操作菜单")
    public BaseResponse<List<SysMenuVo>> menus() {
        List<SysMenuVo> list = menuService.findMenusByUserId();
        return new BaseResponse<>(list);
    }


    //获取当前登录用户信息
    @GetMapping(value = "/getUserInfo")
    @ApiOperation(value = "获取当前登录用户信息")
    public BaseResponse<SysUser> getUserInfo() {
        return new BaseResponse<>(userService.getById(UserContext.currentUser().getUserId()));
    }


    //生成图片验证码
    @GetMapping(value = "/generateValidateCode")
    @ApiOperation(value = "生成图片验证码")
    public BaseResponse<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return new BaseResponse<>(validateCodeVo);
    }

    //用户登录
    @ApiOperation(value = "登录的方法")
    @PostMapping("login")
    public BaseResponse<LoginVo> login(@RequestBody LoginDto loginDto) throws IOException {
        LoginVo loginVo = userService.login(loginDto);
        return new BaseResponse<>(loginVo);
    }

}
